package com.hrms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hrms.pojo.InviteJob;
import com.hrms.service.InviteJobService;

/**
 * @author QHX
 * InviteJobController:招聘管理类,
 * 2017年3月15日
 * 
 */
@Controller
@RequestMapping("/invitejob")
public class InviteJobController {
	@Autowired
	private InviteJobService inviteJobService;
	
	//跳转到修改页面
	@RequestMapping("/alter_invite")
	public String alterInvite(){
		return "inviteJob/alter_invite";
	}
	
	//跳转到添加页面
	@RequestMapping("/add_invite")
	private String addInvite(){
		return "inviteJob/add_invite";
	}
	
	//分页查找
	@RequestMapping("/limit")
	public String getInviteByLimit(HttpServletRequest request,Model model){
		int pageSize=4;
		Object obj=request.getSession().getAttribute("inviteJobList");
		List<InviteJob> inviteJobList=null;
		if(obj==null){
			inviteJobList=this.inviteJobService.selectAll();
			request.getSession().setAttribute("inviteJobList", inviteJobList);
		}else{
			inviteJobList=(List<InviteJob>)obj;
		}
		int total=inviteJobList.size();
		int totalPage=(total%pageSize==0?total/pageSize:total/pageSize+1);
		int showPage=0;
		try{
			showPage=Integer.parseInt(request.getParameter("showPage"));
			if(showPage<=1)
				showPage=1;
		}catch(NumberFormatException e){
			showPage=1;
		}
		if(showPage>totalPage)
			showPage=totalPage;
		int offset=(showPage-1)*pageSize;
		int limit=showPage*pageSize;
		request.getSession().setAttribute("showPage", showPage);
		List<InviteJob> list=this.inviteJobService.selectByLimit(offset, limit);
		model.addAttribute("list",list);
		return "inviteJob/limit_invite";
	}
	
	@RequestMapping(value="/{id}/get")
	public String getInviteJobById(@PathVariable("id")Integer inId,Model model){
		//判断id是否为空
		if(inId==null){
			return "forward:/error/error";
		}
		InviteJob inviteJob=this.inviteJobService.selectByPrimaryKey(inId);
		//验证该id的记录是否存在
		if(inviteJob==null){
			return "forward:/error/error";
		}
		model.addAttribute("inviteJob",inviteJob);
		return "inviteJob/invitejob";
	}
	//根据姓名进行模糊匹配查找
	@RequestMapping(value="/{name}/get")
	public String getInviteJobByName(@PathVariable("name")String name,Model model){
		//验证姓名是否为空
		if(name==null){
			//姓名为空跳转到错误页面
			return "forward:/error/error";
		}
		//获取匹配的所有记录的list
		List<InviteJob> ijList=this.inviteJobService.selectByName(name);
		model.addAttribute("inviteJobList",ijList);
		return "list";
	}
	
	//查找所有记录
	@RequestMapping(value="/selectAll")
	public String selectAll(Model model){
		List<InviteJob> inviteJob=this.inviteJobService.selectAll();
		model.addAttribute("inviteJobList",inviteJob);
		return "inviteJob/listInviteJob";
	}
	
	//添加记录,添加成功跳转到inviteJob/add_succ
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addInviteJob(HttpServletRequest request){
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		int age=Integer.parseInt(request.getParameter("age"));
		String born=request.getParameter("born");
		String job=request.getParameter("job");
		String specialty=request.getParameter("specialty");
		String experience=request.getParameter("experience");
		String education=request.getParameter("education");
		String graduateSchool=request.getParameter("graduateSchool");
		String tel=request.getParameter("tel");
		String address=request.getParameter("address");
		String createTime=request.getParameter("createTime");
		String remarks=request.getParameter("remarks");
		String isHire=request.getParameter("isHire");
		
		InviteJob ij=new InviteJob();
		ij.setName(name);
		ij.setSex(sex);
		ij.setAge(age);
		ij.setBorn(born);
		ij.setJob(job);
		ij.setSpecialty(specialty);
		ij.setExperience(experience);
		ij.setEducation(education);
		ij.setGraduateSchool(graduateSchool);
		ij.setTel(tel);
		ij.setAddress(address);
		ij.setCreateTime(createTime);
		ij.setRemarks(remarks);
		ij.setIsHire(isHire);
		int a=this.inviteJobService.insertInviteJob(ij);
		if(a>0){
			return "inviteJob/add_succ";
		}else{
			return "inviteJob/add_error";
		}
	}
	
	//修改信息
	@RequestMapping(value="/{inId}/alter",method=RequestMethod.POST)
	public String alterInviteJob(HttpServletRequest request,@PathVariable("inId")Integer inId){
		if(inId==null){
			return "forward:/error/error";
		}
		InviteJob ij=this.inviteJobService.selectByPrimaryKey(inId);
		if(ij==null){
			return "forward:/error/error";
		}
		String name=request.getParameter("name");
		if("".equals(name)){
			name=ij.getName();
		}
		String sex=request.getParameter("sex");
		if("".equals(sex)){
			sex=ij.getSex();
		}
		int age=Integer.parseInt(request.getParameter("age"));
		if(age==0){
			age=ij.getAge();
		}
		String born=request.getParameter("born");
		if("".equals(born)){
			born=ij.getBorn();
		}
		String job=request.getParameter("job");
		if("".equals(job)){
			job=ij.getJob();
		}
		String specialty=request.getParameter("specialty");
		if("".equals(specialty)){
			specialty=ij.getSpecialty();
		}
		String experience=request.getParameter("experience");
		if("".equals(experience)){
			experience=ij.getExperience();
		}
		String education=request.getParameter("education");
		if("".equals(education)){
			education=ij.getEducation();
		}
		String graduateSchool=request.getParameter("graduateSchool");
		if("".equals(graduateSchool)){
			graduateSchool=ij.getGraduateSchool();
		}
		String tel=request.getParameter("tel");
		if("".equals(tel)){
			tel=ij.getTel();
		}
		String address=request.getParameter("address");
		if("".equals(address)){
			address=ij.getAddress();
		}
		String createTime=request.getParameter("createTime");
		if("".equals(createTime)){
			createTime=ij.getCreateTime();
		}
		String remarks=request.getParameter("remarks");
		if("".equals(remarks)){
			remarks=ij.getRemarks();
		}
		String isHire=request.getParameter("isHire");
		if("".equals(isHire)){
			isHire=ij.getIsHire();
		}
		ij.setId(inId);
		ij.setName(name);
		ij.setAge(age);
		ij.setJob(job);
		ij.setExperience(experience);
		ij.setTel(tel);
		ij.setAddress(address);
		ij.setBorn(born);
		ij.setCreateTime(createTime);
		ij.setEducation(education);
		ij.setGraduateSchool(graduateSchool);
		ij.setIsHire(isHire);
		ij.setIsHire(isHire);
		ij.setSpecialty(specialty);
		ij.setEducation(education);
		
		int a=this.inviteJobService.updateInviteJob(ij);
		if(a!=0){
			return "inviteJob/alter_succ";
		}else{
			return "inviteJob/alter_error";
		}
	}
	
	//删除信息
	@RequestMapping(value="/{inId}/delete")
	public String deleteInviteJob(@PathVariable("inId")Integer inId){
		if(inId==null){
			return "forward:error/error";
		}
		InviteJob ij=this.inviteJobService.selectByPrimaryKey(inId);
		if(ij==null){
			return "forward:error/error";			
		}
		int a=this.inviteJobService.deleteInviteJob(inId);
		if(a!=0){
			return "inviteJob/delete_succ";
		}else{
			return "inviteJob/delete_error";
		}
	}
}